﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using System.Collections.Generic;

public partial class Manage_Member_GroupItem : System.Web.UI.Page
{
    public List<KeRui.Model.MemberGroup> list;
    private ManageErrMsg nsb = new ManageErrMsg();
    private KeRui.BLL.MemberGroup bll = new KeRui.BLL.MemberGroup();
    protected KeRui.Model.MemberGroup model = new KeRui.Model.MemberGroup();
    protected string ActFlag = "addsave";
    protected string ActName = "添加";
    protected void Page_Load(object sender, EventArgs e)
    {
        //检查登录
        ManageHelper.CheckAdminLogin();
        //检查管理权限
        ManageHelper.CheckAdminPower("system_membermanage");
        list = bll.GetList();
        switch (ManageHelper.PageAct())
        {
            case "addsave":
                AddSave();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;
            default:
                break;
        }
    }

    #region 添加保存
    private void AddSave()
    {
        ManageHelper.CheckAdminPower("system_membergroupadd");//权限
        model.GroupName = PageRequest.GetString("GroupName");
        if (model.GroupName == "")
        {
            nsb.AddErr("请输入组名。");
        }
        model.GroupLevel = PageRequest.GetInt("GroupLevel", -1);
        if (model.GroupLevel == -1)
        {
            nsb.AddErr("组等级应为一个正整数。");
        }
        model.GroupColor = PageRequest.GetString("GroupColor");
        model.EPStart = PageRequest.GetInt("EPStart",-1);
        if (model.EPStart == -1)
        {
            nsb.AddErr("经验开始值应为一个正整数。");
        }
        model.EPEnd = PageRequest.GetInt("EPEnd",-1);
        if (model.EPEnd == -1)
        {
            nsb.AddErr("经验结束值应为一个正整数。");
        }
        model.PublishNum = PageRequest.GetInt("PublishNum", -1);
        if (model.PublishNum == -1)
        {
            nsb.AddErr("信息发布数量应为一个正整数。");
        }
        model.EffectiveDate = PageRequest.GetInt("EffectiveDate", -1);
        if (model.EffectiveDate == -1)
        {
            nsb.AddErr("信息有效期应为一个正整数。");
        }
        nsb.ChkErr();
        if (bll.Add(model)>0)
        {
            ManageHelper.AddLog("system_membergroupadd", "添加了“" + model.GroupName + "”用户组。");
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"GroupList.aspx?sucmsg=添加成功。\";</script>");
        }
        else
        {
            nsb.AddErr("组名称存在相同名称。");
            nsb.ChkErr();
        }
    }
    #endregion

    #region 反显修改页面
    private void Edit()
    {
        ManageHelper.CheckAdminPower("system_membergroupedit");//权限
        int GroupID = PageRequest.GetInt("ID",-1);
        if (GroupID == -1)
        {
            nsb.AddErr("对不起！非正常反问。");
            nsb.ChkErr();
        }
        model = bll.GetModel(GroupID);
        if (model == null)
        {
            nsb.AddErr("对不起！您要修改的组信息不存在。");
            nsb.ChkErr();
        }
        ActFlag = "editsave";
        ActName = "修改";
    }
    #endregion

    #region 修改保存
    private void EditSave()
    {
        ManageHelper.CheckAdminPower("system_membergroupedit");//权限
        model.GroupID = PageRequest.GetInt("GroupID", -1);
        model.GroupName = PageRequest.GetString("GroupName");
        if (model.GroupName == "")
        {
            nsb.AddErr("请输入组名。");
        }
        model.GroupLevel = PageRequest.GetInt("GroupLevel", -1);
        if (model.GroupLevel == -1)
        {
            nsb.AddErr("组等级应为一个正整数。");
        }
        model.GroupColor = PageRequest.GetString("GroupColor");
        model.EPStart = PageRequest.GetInt("EPStart", -1);
        if (model.EPStart == -1)
        {
            nsb.AddErr("经验开始值应为一个正整数。");
        }
        model.EPEnd = PageRequest.GetInt("EPEnd", -1);
        if (model.EPEnd == -1)
        {
            nsb.AddErr("经验结束值应为一个正整数。");
        }
        model.PublishNum = PageRequest.GetInt("PublishNum", -1);
        if (model.PublishNum == -1)
        {
            nsb.AddErr("信息发布数量应为一个正整数。");
        }
        model.EffectiveDate = PageRequest.GetInt("EffectiveDate", -1);
        if (model.EffectiveDate == -1)
        {
            nsb.AddErr("信息有效期应为一个正整数。");
        }
        nsb.ChkErr();
        if (bll.Update(model) > 0)
        {
            ManageHelper.AddLog("system_membergroupadd", "修改了“" + model.GroupName + "”用户组。");
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"GroupList.aspx?sucmsg=修改成功。\";</script>");
        }
        else
        {
            nsb.AddErr("组名称存在相同名称。");
            nsb.ChkErr();
        }
    }
    #endregion

    #region 删除
    private void Del()
    {
        ManageHelper.CheckAdminPower("system_membergroupdel");//权限
        int GroupID = PageRequest.GetInt("ID", -1);
        if (bll.Delete(GroupID) < 0)
        {
            Response.Write("GroupList.aspx?sucmsg=删除失败。"); Response.End();
        }
        ManageHelper.AddLog("system_membergroupdel", "删除了“" + model.GroupName + "”用户组。");
        Response.Redirect("GroupList.aspx?sucmsg=删除成功。");
    }
    #endregion


























    public string MemberGroupLevelImage(int level)
    {
        string img = "";
        int s3 = (int)level / 4;//有多少个3星
        int y = level % 4;//剩余星星

        for (int i=0; i < s3; i++)
        {
            img += ",3";
        }

        if (y == 1)
            img += ",1";
        else if (y == 2)
            img += ",2";
        else if (y == 3)
            img += ",2,1";

        string[] pic = Utils.SplitString(img,",");
        string ret = "";
        for (int i = 1; i < pic.Length; i++)
        {
            ret += "<img align='absmiddle' src='../pic/icons/user/star_level" + pic[i] + ".gif'>";
        }

        return ret.ToString();
    }
}
